package socket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

//客户端
public class Client {
    private Socket socket;            //定义属性
    //构造方法 初始化客户端
    public Client(){
        try{
            System.out.println("正在启动客户端...");
            socket=new Socket("localhost",8090);//localhost 176.136.6.48
            System.out.println("启动客户端成功...");
        }catch (IOException e){
            e.printStackTrace();
        }

    }

    public void start(){
        try {
            //客户端开始工作
            OutputStream out = socket.getOutputStream();
            /**
             * 实现输入一段话 即时发送到服务器端
             */
            Scanner scanner=new Scanner(System.in);
            while(true){
                String line=scanner.next();
                if ("exit".equals(line)){
                    break;
                }
                // String line="我喜欢你安妮";
                byte[]data=line.getBytes(StandardCharsets.UTF_8);
                out.write(data.length);
                out.write(data);
                //  out.write(1);/////00000001
            }

        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            try{
                if(socket!=null){
                    socket.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }

    }
    public static void main(String[] args) {
        Client client=new Client();
        client.start();

    }
}
